package com.raylu.realtime.utils;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.junit.Test;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * <p>
 * Create by lucienoz on 2022/1/13.
 * Copyright © 2022 lucienoz. All rights reserved.
 */
public class KeywordUtil {
    private static Analyzer analyzer = new IKAnalyzer(true);


    public static List<String> getKeyword(String line) throws IOException {
        ArrayList<String> keyWordList = new ArrayList<String>();
        TokenStream keywordIter = analyzer.tokenStream("keyword", line);
        CharTermAttribute termAttribute = keywordIter.addAttribute(CharTermAttribute.class);
        keywordIter.reset();
        while (keywordIter.incrementToken()) {
            keyWordList.add(termAttribute.toString());
        }
        keywordIter.end();
        if (keywordIter!=null) {
            keywordIter.close();
        }

        return keyWordList;
    }

    @Test
    public void testGetKeyword() throws IOException {
        List<String> keyword = getKeyword("这是一个中文分词的例子，你可以直接运行它！IKAnalyer can analysis english text too");
        System.out.println("keyword = " + keyword);
    }

    @Test
    public void testIkSegmenter() throws IOException {
        IKSegmenter ikSegmenter = new IKSegmenter(new StringReader("这是一个中文分词的例子，你可以直接运行它！IKAnalyer can analysis english text too"), true);
        Lexeme next = null;
        while ((next = ikSegmenter.next()) != null) {
            System.out.println(next.getLexemeText());
        }
    }

    public static void main(String[] args) throws IOException {

        // 构建IK分词器，使用smart分词模式
        Analyzer analyzer = new IKAnalyzer(true);

        // 获取Lucene的TokenStream对象
        TokenStream ts = null;
        try {
            ts = analyzer.tokenStream("myfield", new StringReader(
                    "这是一个中文分词的例子，你可以直接运行它！IKAnalyer can analysis english text too"));
            // 获取词元位置属性
            OffsetAttribute offset = ts.addAttribute(OffsetAttribute.class);
            // 获取词元文本属性
            CharTermAttribute term = ts.addAttribute(CharTermAttribute.class);
            // 获取词元文本属性
            TypeAttribute type = ts.addAttribute(TypeAttribute.class);

            // 重置TokenStream（重置StringReader）
            ts.reset();
            // 迭代获取分词结果
            while (ts.incrementToken()) {
                System.out.println(offset.startOffset() + " - " + offset.endOffset() + " : "
                        + term.toString() + " | " + type.type());
            }
            // 关闭TokenStream（关闭StringReader）
            ts.end(); // Perform end-of-stream operations, e.g. set the final offset.

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放TokenStream的所有资源
            if (ts != null) {
                try {
                    ts.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
